package Models.dao;

import Models.enity.Cart;
import Models.utils.DataSourceManager;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class CartDao {

    private QueryRunner runner = new QueryRunner(DataSourceManager.getDataSource());


    public Cart findByCommoitId(String id){
        String sql = "SELECT * FROM cart WHERE commodit_id=?";
        try {
            return runner.query(sql,new BeanHandler<Cart>(Cart.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public int addQuality(String id,int quality){
        String sql = "UPDATE cart SET quality=? WHERE commodit_id=?";
        try {
            return runner.update(sql,quality,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public int insert(String user_id,String commodit_id,String price,String limit_price,String quality){
        String sql = "INSERT INTO cart(user_id,commodit_id,price,limit_price,quality)" +
                " VALUES (?,?,?,?,?)";
        try {
            return runner.update(sql,user_id,commodit_id,price,limit_price,quality);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

}
